package pt.nmfa.peste.server.services.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

public class PesteAction 
{

	protected List convertList(Class<?> toTypeClass,List fromList)
	{
		
		ArrayList toList=new ArrayList();
		
		for(int i=0;i<fromList.size();i++)
		{
			Object destObj=null;
			try {
				destObj = toTypeClass.newInstance();
				BeanUtils.copyProperties(destObj, fromList.get(i));
				toList.add(destObj);
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		

		}
		return toList;
	}
	
	protected List convertSet(Class<?> toTypeClass,Set fromList)
	{
		Iterator it=fromList.iterator();
		ArrayList toList=new ArrayList();
		
		//for(int i=0;i<fromList.size();i++)
		while(it.hasNext())
		{
			Object destObj=null;
			try {
				destObj = toTypeClass.newInstance();
				BeanUtils.copyProperties(destObj, it.next());
				toList.add(destObj);
			} catch (InstantiationException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		

		}
		return toList;
	}
	
	protected Object convertObj(Class<?> toTypeClass,Object fromObj)
	{
		Object toObj=null;
		try {
			toObj = toTypeClass.newInstance();
			BeanUtils.copyProperties(toObj, fromObj);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return toObj;
	}
	protected void copyObj(Object toObj,Object fromObj)
	{
		try {
			BeanUtils.copyProperties(toObj, fromObj);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
